package com.bjtu.literatureclient.service.team.impl;

import com.bjtu.literatureclient.dao.team.ITeamDao;
import com.bjtu.literatureclient.domain.base.BaseModel;
import com.bjtu.literatureclient.domain.team.Team;
import com.bjtu.literatureclient.service.team.ITeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("teamService")
public class TeamServiceImpl implements ITeamService {

    @Autowired
    private ITeamDao teamDao;

//    @Override
//    public List<Team> query(BaseModel baseModel) {
//        return this.teamDao.query(baseModel);
//    }
    @Override
    public List<Team> query(Team team) {
    	return teamDao.query(team);
    }

	/*
	 * @Override public int update(BaseModel baseModel) { return
	 * this.teamDao.update(baseModel); }
	 */

    @Override
    public int insert(Team team) {
        return this.teamDao.update(team);
    }

	@Override
	public int delete(BaseModel baseModel) {
		// TODO Auto-generated method stub
		 return this.teamDao.delete(baseModel);
	}
	
	/* 
	* (Sikai Lin: 2020年2月5日)
	* @Description: 制造一个假的团队，主要是创建时间（方便管理人员清理多余假团队）、状态 -1
	* @param id: 占用团队号
	* @return: 假团队
	*/
	Team getFakeTeam(String id) {
		Team team = new Team();
		team.setTeamNo(id);
		team.setUserEmail("123456789@email.cn");
		long l = System.currentTimeMillis();
        Date date = new Date(l);
        SimpleDateFormat createTimeFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat updateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = createTimeFormat.format(date);
        String updateTime = updateTimeFormat.format(date);
        team.setDate(createTime);
        team.setDateTime(updateTime);
        team.setTeamState("-1");
		return team;
	}

    /* 
    * (Sikai Lin: 2020年2月5日)
    * @Description: 获取新团队id，并往数据库插入一条状态为-1的团队记录
    * @return:  新团队id
    */
    @Override
    public String getNewTeamNo() {
        Team team = teamDao.getMaxIdTeam();
        
        Integer maxId = 0;
        if (team != null) {
            maxId = Integer.parseInt(team.getTeamNo().substring(1));
        }
        String fakeTeamNo = "T" + String.format("%06d", maxId+1);
        Team fakeTeam = getFakeTeam(fakeTeamNo);
        teamDao.insert(fakeTeam);
        
        return fakeTeamNo;
    }

	@Override
	public int checkTeamState(String teamNo) {
		return teamDao.checkTeamState(teamNo);
	}

    @Override
    public Team selectTeamById(String teamNo) {
        Team team = teamDao.selectTeamById(teamNo);
        return team;
    }

	@Override
	public String checkTeamEmail(String teamEmail) {
		List<Team> team = teamDao.selectTeamByEmail(teamEmail);
		if (team.size() == 0) {
			return "y";
		}
		return "n";
	}

	/* 
	* (Sikai Lin: 2020年2月19日)
	* @Description: 根据teamNo更新团队信息
	* @param team 团队新信息
	* @return: 更改的条数
	*/
	@Override
	public int update(Team team) {
		return teamDao.update(team);
	}
}
